/*
 *  Copyright 2010 mazhao.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package shopping.commons;

import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import shopping.model.Catalog;
import shopping.model.LoginInfo;
import shopping.model.ShoppingCart;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: mazhao
 * Date: 2010/11/09
 * Time: 23:56:23
 * To change this template use File | Settings | File Templates.
 */
public final class Util {

    private static Log log = LogFactory.getLog(Util.class);

    /**
     * get http session belongs to current user.
     *
     * @return Map of session data.
     */
    private static Map getSession() {
        return ActionContext.getContext().getSession();
    }

    /**
     * get http application object of the current web application.
     *
     * @return
     */
    private static Map getApplication() {
        return ActionContext.getContext().getApplication();
    }

    /**
     * Utility class for login info operation.
     */
    public static class Security {

        /**
         * save login info to session
         *
         * @return
         */
        public static boolean saveUserToSession(LoginInfo login) {
            getSession().put(LoginInfo.SK_LOGIN_INFO, login);
            return true;
        }

        /**
         * remove login info from session.
         *
         * @return
         */
        public static boolean removeUserFromSession() {
            getSession().remove(LoginInfo.SK_LOGIN_INFO);
            return true;
        }

        /**
         * get login info from session.
         *
         * @return
         */
        public static LoginInfo retriveUserFromSession() {
            return (LoginInfo) getSession().get(LoginInfo.SK_LOGIN_INFO);
        }
    }

    /**
     * Utility class for shopping cart operation.
     */
    public static class Shopping {

        /**
         * get shopping cart from session.
         *
         * @return
         */
        public static ShoppingCart retriveShoppingCart() {
            return (ShoppingCart) getSession().get(ShoppingCart.SK_SHOPPING_CART);
        }

        /**
         * remove shopping cart from session.
         *
         * @return
         */
        public static boolean removeShoppingCart() {
            getSession().remove(ShoppingCart.SK_SHOPPING_CART);
            return true;
        }

        /**
         * save shopping cart to the session of current user
         *
         * @param sc
         * @return
         */
        public static boolean saveShoppingCart(ShoppingCart sc) {
            getSession().put(ShoppingCart.SK_SHOPPING_CART, sc);
            return true;
        }

        /**
         * if has shopping cart.
         *
         * @return
         */
        public static boolean hasShoppingCart() {
            return getSession().containsKey(ShoppingCart.SK_SHOPPING_CART);
        }
    }


    public static class Menu {

        private static final String SK_MENU = "sk_front_ment";

        public static String getMenu() {
            // get menu from application
            Map app = getApplication();

            if(!app.containsKey(SK_MENU)) {
                invalidateMenu();
            }
            
            String menu = (String) app.get(SK_MENU);
            return menu;
        }

        public static boolean invalidateMenu() {
            List<Catalog> cats = Catalog.selectAllCatalog();
            String menu = buildMenu(cats);
            Map app = getApplication();
            app.remove(SK_MENU);
            app.put(SK_MENU, menu);
            log.debug("build new menu:" + menu);
            return true;
        }

        private static final String LINE_SEP = System.getProperty("line.separator");

        private static String buildMenu(List<Catalog> cats) {
            StringBuilder sb = new StringBuilder();
            sb.append("<h3 class='alt'>Main Menu</h2>").append(LINE_SEP).append("<ul id='mainMenu' class='sf-menu sf-vertical'>").append(LINE_SEP);
            for (int i = 0; i < cats.size(); i++) {
                Catalog cur = cats.get(i);
                if (i < cats.size() - 1) {
                    Catalog nxt = cats.get(i + 1);
                    if (cur.getDepth() < nxt.getDepth()) {
                        sb.append("<li class='current'>").append(LINE_SEP).append("  <a class='sf-with-ul alt' href='./catalog.action?catalogId=" + cur.getId() + "'>" + cur.getName() + "</a>").append(LINE_SEP).append("  <ul>").append(LINE_SEP);
                    } else if (cur.getDepth() == nxt.getDepth()) {
                        sb.append("    <li><a href='./catalog.action?catalogId=" + cur.getId() + "' class='alt'>" + cur.getName() + "</a></li>").append(LINE_SEP);
                    } else {
                        sb.append("    <li><a href='./catalog.action?catalogId=" + cur.getId() + "' class='alt'>" + cur.getName() + "</a></li>").append(LINE_SEP).append("  </ul>").append(LINE_SEP).append("</li>").append(LINE_SEP);
                    }
                } else {
                    sb.append("    <li><a href='./catalog.action?catalogId=" + cur.getId() + "' class='alt'>" + cur.getName() + "</a></li>").append(LINE_SEP).append("  </ul>").append(LINE_SEP).append("</li>").append(LINE_SEP).append("</ul>").append(LINE_SEP);
                }
            }
            return sb.toString();
        }
    }

    private Util() {
    }
}
